package com.iotproject.educenter.service.impl;

import com.iotproject.commonutils.JwtUtils;
import com.iotproject.commonutils.MD5;
import com.iotproject.educenter.entity.Member;
import com.iotproject.educenter.entity.vo.RegisterVo;
import com.iotproject.educenter.mapper.UcenterMemberMapper;
import com.iotproject.educenter.service.UcenterMemberService;
import com.iotproject.servicebase.exceptionhandler.GlobalException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-12
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, Member> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(Member member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            //其中一个为空
            throw new GlobalException(20001, "登录失败");
        }
        //根据手机号查询用户
        QueryWrapper<Member> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile", mobile);
        Member user = baseMapper.selectOne(ucenterMemberQueryWrapper);;
        if (user == null) {
            throw new GlobalException(20001, "用户不存在");
        }
        if (!MD5.encrypt(password).equals(user.getPassword())) {
            throw new GlobalException(20001, "密码错误");
        }
        //判断用户是否禁用
        if (user.getIsDisabled()) {
            throw new GlobalException(20001, "用户被禁用");
        }
        //更改gmt_modified，把它当作登陆时间
        user.setGmtModified(new Date());
        baseMapper.updateById(user);
        //根据id，nickName生成JwtToken
        return JwtUtils.getJwtToken(user.getId(), user.getNickname());
    }

    @Override
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //判空
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(code)) {
            throw new GlobalException(20001, "注册失败");
        }
        //获取redis里的验证码
        String res = redisTemplate.opsForValue().get(mobile);
        if (!res.equals(code)) {//不相等
             throw new GlobalException(20001, "注册失败，验证码输入错误");
        }
        //判断手机号是否存在
        QueryWrapper<Member> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(ucenterMemberQueryWrapper);
        if (count > 0) {
            throw new GlobalException(20001, "注册失败,该用户已存在");
        }
        //添加到数据库
        Member user = new Member(nickname, mobile, MD5.encrypt(password),false );
        //设置默认用户头像
        user.setAvatar("https://read-picture-001.oss-cn-beijing.aliyuncs.com/user-head.jpg");
        baseMapper.insert(user);
    }

    //根据openid查询是否存在这个用户
    @Override
    public Member getOpenIdMember(String openid) {
        QueryWrapper<Member> ucenterMemberQueryWrapper = new QueryWrapper<>();
        ucenterMemberQueryWrapper.eq("openid", openid);
        Member member = baseMapper.selectOne(ucenterMemberQueryWrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

    @Override
    public void updateNickname(String userId, String newName) {
        baseMapper.updateNickname(userId, newName);
    }

    @Override
    public void updateAvatar(String userId, String avatar) {
        baseMapper.updateAvatar(userId, avatar);
    }

    @Override
    public void updatePassword(String userId, String password) {
        String newPassword = MD5.encrypt(password);
        baseMapper.updatePassword(userId, newPassword);
    }

    @Override
    public void updateMobile(String userId, String mobile) {
        baseMapper.updateMobile(userId, mobile);
    }

    @Override
    public Integer countLoginDay(String day) {
        return baseMapper.countLoginDay(day);
    }
}
